package com.heyqing.topic.LongestPalindrome_0409;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * ClassName:LongestPalindrome
 * Package:com.heyqing.topic.LongestPalindrome_0409
 * Description:
 *
 * @Date:2024/9/29
 * @Author:Heyqing
 */
public class LongestPalindrome {
    public int longestPalindrome(String s) {
        int maxLen = 0;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), Objects.isNull(map.get(s.charAt(i))) ? 1 : map.get(s.charAt(i)) + 1);
        }
        boolean isExistOdd = false;
        for (Integer value : map.values()) {
            if (value % 2 == 0) maxLen += value;
            else {
                isExistOdd = true;
                maxLen += value - 1;
            }
        }
        if (isExistOdd) maxLen++;
        return maxLen;
    }
}
